package com.carleasoft.mps.cms.business.collector.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.collector.bean.entity.CollectorEntity;
import com.carleasoft.mps.cms.business.collector.bean.po.CollectorPos;
import com.carleasoft.mps.cms.business.collector.dao.CollectorMapper;
import com.carleasoft.mps.cms.business.resource.bean.entity.LibraryEntity;
import com.carleasoft.mps.cms.business.resource.bean.entity.ReasourceEntity;
import com.carleasoft.mps.cms.business.resource.service.LibraryService;
import com.carleasoft.mps.cms.business.resource.service.ReasourceService;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: yangHao
 * @createDate:2023/6/17
 * @description:
 */
@Service
public class CollectorServiceImpl extends ServiceImpl<CollectorMapper, CollectorEntity> implements CollectorService {

    @Autowired
    private ReasourceService reasourceService;

    @Autowired
    private LibraryService libraryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCollector(CollectorEntity collectorEntity) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        collectorEntity.setCreateTime(LocalDateTime.now());
        collectorEntity.setUserCode(user.getUserCode());
        collectorEntity.setUserName(user.getUserName());
        return this.save(collectorEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(CollectorEntity collectorEntity) {
        return this.remove(Wrappers.<CollectorEntity>lambdaQuery()
                .eq(CollectorEntity::getCode, collectorEntity.getCode())
                .eq(CollectorEntity::getUserCode, collectorEntity.getUserCode()));
    }

    @Override
    public Page<CollectorEntity> collectorList(Page page, CollectorPos collectorEntity) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        QueryWrapper<CollectorEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_CODE", user.getUserCode());
        if (collectorEntity.getType() != null) {
            queryWrapper.eq("TYPE", collectorEntity.getType());
            if (collectorEntity.getType()!=null && collectorEntity.getType()==1){
                List<CollectorEntity> collectorEntities = this.list(Wrappers.<CollectorEntity>lambdaQuery()
                        .eq(CollectorEntity::getType, 1));
                if (CollUtil.isNotEmpty(collectorEntities)){
                    List<String> codes = collectorEntities.stream().map(CollectorEntity::getCode).collect(Collectors.toList());
                    List<ReasourceEntity> reasourceEntities = reasourceService.list(Wrappers.<ReasourceEntity>lambdaQuery()
                            .in(ReasourceEntity::getReasourceCode, codes));
                    Set<String> libraryCodes = reasourceEntities.stream().map(ReasourceEntity::getRLibrary).collect(Collectors.toSet());
                    List<LibraryEntity> libraryEntities = libraryService.list(Wrappers.<LibraryEntity>lambdaQuery()
                            .in(LibraryEntity::getLibraryCode, libraryCodes)
                            .eq(LibraryEntity::getIsShow,1));
                    List<String> newLibraryCodes = libraryEntities.stream().map(LibraryEntity::getLibraryCode).collect(Collectors.toList());
                    List<ReasourceEntity> newReasourceEntities = reasourceService.list(Wrappers.<ReasourceEntity>lambdaQuery()
                            .in(ReasourceEntity::getReasourceCode, codes)
                    .in(ReasourceEntity::getRLibrary,newLibraryCodes));
                    List<String> newResourceCodes = newReasourceEntities.stream().map(ReasourceEntity::getReasourceCode).collect(Collectors.toList());
                    queryWrapper.in("CODE",newResourceCodes);
                }

                queryWrapper.orderByDesc("CREATE_TIME");


            }
            return this.page(page, queryWrapper);
        }
        return this.page(page, queryWrapper);
    }

}

